﻿using System;
using System.Collections;
using System.Globalization;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace CodePlex.TeamFoundation.Adapters.WorkItemTracking
{
  /// <summary>
  /// Adapter that implements the IWorkItem interface for 
  /// Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore
  /// </summary>
  public class WorkItemStoreAdapter : IWorkItemStore
  {
    #region Constructors

    private WorkItemStoreAdapter() 
    {
      // Hide the default constuctor
    }

    public WorkItemStoreAdapter(WorkItemStore sourceWorkItemStore)
    {
      tfsWorkItemStore = sourceWorkItemStore;
      tfsWorkItemStore.ImportEventHandler += this.ImportEventHandler;
      tfsWorkItemStore.MetadataChanged += this.MetadataChanged;
      tfsWorkItemStore.MetadataChanging += this.MetadataChanging;
    }

    #endregion
    
    #region Private Variables

    WorkItemStore tfsWorkItemStore;
    
    #endregion

    #region IWorkItemStore Members

    public event ImportEventHandler ImportEventHandler;

    public event MetadataChangeEventHandler MetadataChanged;

    public event MetadataChangeEventHandler MetadataChanging;

    public string CallingProcessIdentity
    {
      get
      {
        return tfsWorkItemStore.CallingProcessIdentity;
      }
      set
      {
        tfsWorkItemStore.CallingProcessIdentity = value;
      }
    }

    public CultureInfo CultureInfo
    {
      get { return tfsWorkItemStore.CultureInfo; }
    }

    public WorkItemStoreDiagnostics Diagnostics
    {
      get { return tfsWorkItemStore.Diagnostics; }
    }

    public FieldDefinitionCollection FieldDefinitions
    {
      get { return tfsWorkItemStore.FieldDefinitions; }
    }

    public ProjectCollection Projects
    {
      get { return tfsWorkItemStore.Projects; }
    }

    public RegisteredLinkTypeCollection RegisteredLinkTypes
    {
      get { return tfsWorkItemStore.RegisteredLinkTypes; }
    }

    public Microsoft.TeamFoundation.Client.TeamFoundationServer TeamFoundationServer
    {
      get { return tfsWorkItemStore.TeamFoundationServer; }
    }

    public TimeZone TimeZone
    {
      get { return tfsWorkItemStore.TimeZone; }
    }

    public UserDisplayMode UserDisplayMode
    {
      get { return tfsWorkItemStore.UserDisplayMode; }
    }

    public BatchSaveError[] BatchSave(WorkItem[] workitems)
    {
      return tfsWorkItemStore.BatchSave(workitems);
    }

    public System.Xml.XmlDocument ExportGlobalLists()
    {
      return tfsWorkItemStore.ExportGlobalLists();
    }

    public Uri[] GetReferencingWorkItemUris(Uri referencedUri)
    {
      return tfsWorkItemStore.GetReferencingWorkItemUris(referencedUri);
    }

    public StoredQuery GetStoredQuery(Guid guid)
    {
      return tfsWorkItemStore.GetStoredQuery(guid);
    }

    public IWorkItem GetWorkItem(int id)
    {
      return new WorkItemAdapter(tfsWorkItemStore.GetWorkItem(id));
    }

    public IWorkItem GetWorkItem(Uri uri)
    {
      return new WorkItemAdapter(tfsWorkItemStore.GetWorkItem(uri));
    }

    public IWorkItem GetWorkItem(int id, DateTime at)
    {
      return new WorkItemAdapter(tfsWorkItemStore.GetWorkItem(id, at));
    }

    public IWorkItem GetWorkItem(int id, int revision)
    {
      return new WorkItemAdapter(tfsWorkItemStore.GetWorkItem(id, revision));
    }

    public IWorkItem GetWorkItem(Uri uri, DateTime at)
    {
      return new WorkItemAdapter(tfsWorkItemStore.GetWorkItem(uri, at));
    }

    public IWorkItem GetWorkItem(Uri uri, int revision)
    {
      return new WorkItemAdapter(tfsWorkItemStore.GetWorkItem(uri, revision));
    }

    public void ImportGlobalLists(string lists)
    {
      tfsWorkItemStore.ImportGlobalLists(lists);
    }

    public void ImportGlobalLists(System.Xml.XmlElement listsElement)
    {
      tfsWorkItemStore.ImportGlobalLists(listsElement);
    }

    public WorkItemCollection Query(string wiql)
    {
      return tfsWorkItemStore.Query(wiql);
    }

    public WorkItemCollection Query(BatchReadParameterCollection batchReadParams, string wiql)
    {
      return tfsWorkItemStore.Query(batchReadParams, wiql);
    }

    public WorkItemCollection Query(string wiql, IDictionary context)
    {
      return tfsWorkItemStore.Query(wiql, context);
    }

    public int QueryCount(string wiql)
    {
      return tfsWorkItemStore.QueryCount(wiql);
    }

    public int QueryCount(string wiql, IDictionary context)
    {
      return tfsWorkItemStore.QueryCount(wiql, context);
    }

    public void RefreshCache()
    {
      tfsWorkItemStore.RefreshCache();
    }

    public bool SyncToCache()
    {
      return tfsWorkItemStore.SyncToCache();
    }

    #endregion
  }
}
